package com.lipuwater.agent.ui.activity;

import android.view.View;

import com.ismartlib.utils.NetUtils;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.adapter.BaseListAdapter;
import com.lipuwater.agent.app.base.ui.BaseListActivity;
import com.lipuwater.agent.app.base.utils.ListSettings;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;
import com.lipuwater.agent.http.bean.message.MessageInfoBean;
import com.lipuwater.agent.http.bean.message.MessageListBean;
import com.lipuwater.agent.ui.adapter.ATMessageAdapter;
import com.lipuwater.agent.utils.MapToBeanUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class MessageListActivity extends BaseListActivity<MessageInfoBean> {
    private int PAGE_INDEX = 1;
    private int PAGE_SIZE = 10;

    private boolean isShowLoading = true;

    @Override
    protected void OnViewCreated() {
        mViewBuilder.mRecyclerview.setBackgroundColor(0xffffffff);
        viewToolbarShadow.setVisibility(View.VISIBLE);
        initTitle(R.drawable.icon_aleft, "消息");
        showLoading();
    }

    @Override
    public void requestData(boolean needRefresh) {
        if (needRefresh) {
            PAGE_INDEX = 1;
        }
        if (!isShowLoading) {
            isShowLoading = true;
            showLoading();
        }
        getMessageList();
    }

    @Override
    protected BaseListAdapter<MessageInfoBean> getListAdapter() {
        return new ATMessageAdapter(this);
    }

    @Override
    protected ListSettings getBaseSettings() {
        ListSettings settings = new ListSettings();
        return settings;
    }

    private void getMessageList() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        params.put("pageIndex", PAGE_INDEX);
        params.put("pageSize", PAGE_SIZE);
        ApiRequestsBasis.getMessage(this, params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                if (result.getStatus() == HttpConfig.success) {
                    if (isShowLoading) {
                        hideEmptyLayout();
                    }
                    setData(result);
                } else {
                    if (isShowLoading) {
                        isShowLoading = false;
                        showNoNetWork();
                    }
                    Toast(result.getMessage());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                if (isShowLoading) {
                    isShowLoading = false;
                    showNoNetWork();
                }
                onDataLoaded(null);//没网络记得调用onDataLoaded，否则导致刷新被禁止
                if (!NetUtils.isConnected(MessageListActivity.this)) {
                    Toast(getString(R.string.network_fails));
                } else {
                    Toast(getString(R.string.service_error));
                }
            }
        });
    }

    private void setData(ResponseParams result) {
        MessageListBean messageListBean = (MessageListBean) MapToBeanUtil.toJavaBean(result.getParams(), MessageListBean.class);
        if (messageListBean != null && messageListBean.getList() != null) {
            PAGE_INDEX++;
            onDataLoaded(messageListBean.getList());
            if (mAdapter.getContentItemCount() == 0) {
                showNoData("暂无相关消息");
            }
        } else {
            onDataLoaded(null);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ApiRequestsBasis.cancelRequests(this);
    }
}
